//
//  LoginViewController.m
//  Otaku
//
//  Created by lanou3g on 16/5/12.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisteredViewController.h"

@interface LoginViewController ()
//邮箱
@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
//密码
@property (weak, nonatomic) IBOutlet UITextField *passWordTextFiled;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage *agreementImage = [UIImage imageNamed:@"return_personal@2x.png"];
    UIImage *newAgreementImage = [agreementImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:newAgreementImage style:(UIBarButtonItemStylePlain) target:self action:@selector(agreementAction:)];
    
    self.navigationItem.leftBarButtonItem = item;

    // Do any additional setup after loading the view from its nib.
}
- (IBAction)didClickLoginBut:(UIButton *)sender {
    
    NSURL *url = [NSURL URLWithString:@"http://api2.pianke.me/user/login"];
    NSMutableURLRequest *mReuest = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    [mReuest setHTTPMethod:@"POST"];
    //设置请求方式
    NSString *boduStr = [NSString stringWithFormat:@"email=%@&passwd=%@",self.emailTextField.text,self.passWordTextFiled.text];
    NSData *body = [boduStr dataUsingEncoding:NSUTF8StringEncoding];
    [mReuest setHTTPBody:body];
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mReuest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSString *msg = jsonDict[@"data"][@"msg"];
            msg = msg?msg:@"登录成功";
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:(UIAlertControllerStyleAlert)];
            [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if ([jsonDict[@"result"] isEqual:@1]) {
                    
                    [self dismissViewControllerAnimated:YES completion:nil];
                    
                }
            }]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alertVC animated:YES completion:nil];
            });
        }
    }];
    [dataTask resume];

}

//返回上一页点击事件
- (void)agreementAction:(UIBarButtonItem *)but {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//注册按钮点击事件
- (IBAction)registeredButtonAction:(UIButton *)sender {
    
    RegisteredViewController *registeredVC = [[RegisteredViewController alloc]init];
    
    [self.navigationController pushViewController:registeredVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
